﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using haptic.src.engine;
using haptic.src.engine.debug;
using haptic.src.engine.collisions;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using haptic.src.engine.core;

namespace haptic.src.components
{
    [Serializable]
    public class cCollider : Component
    {
        public cCollider(){}
        public cCollider(Entity oOwner) : base(oOwner)
        {
            Vector2 vRadius = oOwner.m_vSize * .5f;
            m_oCollider = new AABBCollider(oOwner.m_vPosition/* + vRadius*/, vRadius);
        }
        /// <summary>
        /// not working
        /// </summary>
        public bool IsColliding(Entity oEntity)
        {
            if (m_oCollider == null)
                return false;
            cCollider oCollider = (cCollider)oEntity.GetComponent("cCollider");
            if (oCollider == null)
                return false;
            return m_oCollider.IsColliding(oCollider.m_oCollider);
        }
        #region Load
        public override Result Load(haptic.src.engine.Scene oGame, Microsoft.Xna.Framework.Content.ContentManager oContent)
        {
            m_oCollisionManager = oGame.m_oCollisionManager;
            m_oCollisionManager.AddCollider(m_oCollider, CollisionManager.ColliderFlags.E_DEFAULT);
            return Result.Success;
        }
        #endregion

        #region Update
        public override void Update(GameTime oTime)
        {
            if (m_oCollider != null)
            {
                Vector2 vRadius = m_oEntity.m_vSize * .5f;
                AABBCollider oCollider = (AABBCollider)m_oCollider;
                oCollider.Set(m_oEntity.m_vPosition/* + vRadius*/, vRadius);
            }
            base.Update(oTime);
        }
        #endregion

        #region Draw
        public override void DrawDebug(DebugRenderer oDebug, GameTime oTime)
        {
            if (m_oCollider != null)
            {
                m_oCollider.DrawDebug(oDebug);
            }
        }
        #endregion
        #region Attributes
        private ICollider m_oCollider;
        public ICollider oCollider { get { return m_oCollider; } }
        private CollisionManager m_oCollisionManager;
        public CollisionManager oCollisionManager { get { return m_oCollisionManager; } }
        #endregion
    }
}
